﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDX.Direct2D1;
using SharpDX;

namespace SFSpacefight
{
	public abstract class Screen
	{
		public const double transitionframes = 10;
		public const double invtransitionframes = 1d / transitionframes;

		protected List<Control> objects = new List<Control>();
		protected double progresscountdown = 0;
		protected double regresscountdown = 0;
		protected Screen nextscreen;

		protected RenderTarget mytarget; //HACK JOB

		public Screen(RenderTarget intarget)
		{
			mytarget = intarget;
		}
		public virtual GameState Loop(int mousex, int mousey, bool clicked)
		{
			if (progresscountdown > 0)
			{
				progresscountdown--;

				if (progresscountdown == 0)
					return GameState.NextScreen;
				return GameState.Transitioning;
			}
			else if (regresscountdown > 0)
			{
				regresscountdown--;

				if (regresscountdown == 0)
					return GameState.NextScreen;
				return GameState.Transitioning;
			}

			return GameState.None;
		}
		public virtual void Draw(RenderTarget target, double percent)
		{
			Matrix3x2 backuptransform = target.Transform;

			if (progresscountdown > 0)
			{
				target.Transform = Matrix3x2.Translation(Methods.smoothstep(0, 0, Game.width, 0, (progresscountdown - percent) * invtransitionframes), 0f);
				nextscreen.Draw(target, percent);
				target.Transform = Matrix3x2.Translation(Methods.smoothstep(0, 0, -Game.width, 0, (transitionframes - progresscountdown + percent) * invtransitionframes), 0f);
			}
			else if (regresscountdown > 0)
			{
				target.Transform = Matrix3x2.Translation(Methods.smoothstep(0, 0, -Game.width, 0, (regresscountdown - percent) * invtransitionframes), 0f);
				nextscreen.Draw(target, percent);
				target.Transform = Matrix3x2.Translation(Methods.smoothstep(0, 0, Game.width, 0, (transitionframes - regresscountdown + percent) * invtransitionframes), 0f);
			}
			for (int i = 0; i < objects.Count; i++)
			{
				objects[i].Draw(target, percent);
			}

			target.Transform = backuptransform;
		}
		public Screen NextScreen()
		{
			return nextscreen;
		}
	}
}
